<?php

namespace backend\components;

use yii\base\Exception;
use yii\web\Controller;
use Yii;

/**
 * Created by PhpStorm.
 * User: david
 * Date: 2016/12/7
 * Time: 15:11
 * Email:liyongsheng@meicai.cn
 */
class BackendController extends Controller
{

    /**
     * 设置页面提示信息
     * @param string $message
     * @param string $type
     * @param array|string $url
     * @return \yii\web\Response the current response object
     */
    public function showFlash($message, $type = 'danger', $url = null)
    {
        $this->addFlash($message, $type, true);

        if ($url == null) {
            return $this->refresh();
        }
        return $this->redirect($url);
    }

    /**
     * 添加页面提示信息
     * @param string|array $message
     * @param string $type
     * @param bool $removeAfterAccess
     */
    public function addFlash($message, $type = 'danger', $removeAfterAccess = true)
    {
        Yii::$app->session->setFlash($type, $message, $removeAfterAccess);
    }

    /**
     * 错误提示
     */
    public function error($message)
    {
        throw new Exception($message);
    }

    /**
     * 变量不能为空
     */
    public function noempty($data, $message = '参数有误')
    {
        if (!$data) {
            throw new Exception($message);
        }
        return $data;
    }
}